package com.example.cunion.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cunion.entity.Complaint;
import com.example.cunion.entity.Complaint;
import com.example.cunion.entity.Message;
import com.example.cunion.exception.CunionException;
import com.example.cunion.mapper.ShopMapper;
import com.example.cunion.service.ComplaintService;
import com.example.cunion.mapper.ComplaintMapper;
import com.example.cunion.util.MessageTask;
import com.example.cunion.util.StringSnowflakeIdGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
* @author Aero
* @description 针对表【complaint(投诉表)】的数据库操作Service实现
* @createDate 2023-10-22 22:37:28
*/
@Service
public class ComplaintServiceImpl extends ServiceImpl<ComplaintMapper, Complaint>
    implements ComplaintService{

    @Resource
    private ComplaintMapper complaintMapper;
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private MessageTask messageTask;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public List<HashMap> searchUnCheckFirstComplaintByPage(HashMap map) {
        List<HashMap> list = complaintMapper.searchUnCheckFirstComplaintByPage(map);
        return list;
    }

    @Override
    public List<HashMap> searchUnCheckByPage(HashMap map) {
        List<HashMap> list = complaintMapper.searchUnCheckComplaintByPage(map);
        return list;
    }

    @Override
    public Integer agreeComplaint(String id) {
        Integer result = complaintMapper.agreeComplaint(id);
        if (result != 1){
            throw new CunionException("审核失败！");
        }
        Message message = new Message();
        String shopId = complaintMapper.searchShopIdByComplaintId(id);
        String userId = shopMapper.searchUserIdByShopId(shopId);
        String string = complaintMapper.searchUserIdByComplaintId(id);
        HashMap<String, Object> objectHashMap = new HashMap<>();
        objectHashMap.put("shopId", shopId);
        objectHashMap.put("status", "整改中");
        Integer result1 = shopMapper.updateShopStatus(objectHashMap);
        if (result1 != 1){
            throw new CunionException("修改店铺状态失败！");
        }
        StringSnowflakeIdGenerator generator = new StringSnowflakeIdGenerator(1, 1);
        String nextId = generator.nextId();
        String content = "您的店铺被初步判断有存在价格或者卫生方面的问题，目前正处于整改中，平台将扣除您的平台押金。如想重新上架该店铺，请重新提交店铺申请";
        message.setId(nextId);
        message.setReceiverId(userId);
        message.setIsCheck(1);
        HashMap<String, Object> map = new HashMap<>();
        map.put("shopId", id);
        map.put("content", content);
        map.put("avatar", "http://eb118-file.cdn.bcebos.com/upload/1E63D742ADAD22DDD55B9AA050964F6E?x-bce-process=style/wm_ai");
        map.put("sendTime", DateUtil.current());
        map.put("result", "店铺整改中");
        // 使用ObjectMapper将HashMap转换为JSON字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = null;
        try {
            jsonString = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        // 将JSON字符串设置为消息内容
        message.setContent(jsonString);
        messageTask.sendAsync(userId, message);

        String nextedId = generator.nextId();
        String content1 = "您的投诉的店铺经证实存在问题，现已下架整改，请关注后续结果，感谢您的监督！";
        message.setId(nextedId);
        message.setReceiverId(string);
        message.setIsCheck(1);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("shopId", id);
        hashMap.put("content", content1);
        hashMap.put("avatar", "http://eb118-file.cdn.bcebos.com/upload/1E63D742ADAD22DDD55B9AA050964F6E?x-bce-process=style/wm_ai");
        hashMap.put("sendTime", DateUtil.current());
        hashMap.put("result", "已告知用户投诉成功");
        try {
            jsonString = objectMapper.writeValueAsString(hashMap);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        // 将JSON字符串设置为消息内容
        message.setContent(jsonString);
        messageTask.sendAsync(string, message);
        Set keys = redisTemplate.keys("shop:searchAllShops:*");
        if (keys != null){
            redisTemplate.delete(keys);
        }
        return result;
    }

    @Override
    public Integer rejectComplaint(String id) {
        Integer result = complaintMapper.rejectComplaint(id);
        if (result != 1){
            throw new CunionException("审核失败！");
        }
        return result;
    }

    @Override
    public Integer addComplaint(HashMap map) {
        Integer result = complaintMapper.addComplaint(map);
        if (result != 1){
            throw new CunionException("添加投诉失败！");
        }
        return result;
    }

    @Override
    public Integer updateComplaintStatus(HashMap map) {
        Integer result = complaintMapper.updateComplaintStatus(map);
        if (result != 1){
            throw new CunionException("更新投诉状态失败！");
        }
        return result;
    }

    @Override
    public Integer addComplaint(Complaint complaint) {
        int insert = complaintMapper.insert(complaint);
        if (insert != 1){
            throw new CunionException("添加失败");
        }
        return insert;
    }

    @Override
    public Integer deletedComplaint(String complaintId) {
        int result = complaintMapper.deleteById(complaintId);
        if (result != 1){
            throw new CunionException("删除失败！");
        }
        return result;
    }

    @Override
    public Integer updateComplaint(Complaint complaint) {
        int result = complaintMapper.updateById(complaint);
        if (result != 1){
            throw new CunionException("修改失败");
        }
        return result;
    }

    @Override
    public List<Complaint> searchComplaint(Integer start, Integer length) {
        Page<Complaint> page = new Page<>(start, length);
        QueryWrapper<Complaint> QueryWrapper = new QueryWrapper<>();
        Page<Complaint> selectPage = complaintMapper.selectPage(page, QueryWrapper);
        List<Complaint> list = selectPage.getRecords();
        return list;
    }
}




